package com.weasing.res.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.MDC;

import com.weasing.res.domain.WxAdmin;

/**
 * 有关日志的过滤器
 * 
 * @author 黄龙
 */

public class LogFilter implements Filter {

	private final static double DEFAULT_USERID = Math.random() * 100000.0;

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		if (session == null) {
			MDC.put("operatorid", "");
		} else {
			WxAdmin wxAdmin = (WxAdmin) session.getAttribute("wxAdmin");
			if (wxAdmin == null) {
				MDC.put("operatorid", "");
			} else {
				MDC.put("operatortype", wxAdmin.getRoleid());
				MDC.put("operatorid", wxAdmin.getId());
				MDC.put("truename", wxAdmin.getTruename());
			}
		}
		// Log logger = LogFactory.getLog(this.getClass());
		// logger.info("过滤器记录的,,,测试用 请无视");
		chain.doFilter(request, response);
	}

	public void init(FilterConfig Config) throws ServletException {
		// this.filterConfig = Config;
		// String ccc = Config.getServletContext().getInitParameter("cherset");
		// this.targetEncoding = Config.getInitParameter("cherset");

	}
}
